from app import db


# 模型基类
class BaseModel(db.Model):
    # 只继承,不执行
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    def save(self, commit=True):
        db.session().add(self)
        if commit:
            db.session().commit()

    def update(self):
        db.session().commit()

    def delete(self):
        db.session().delete(self)
        db.session().commit()


# 员工
class Person(BaseModel):
    # 用户名
    uname = db.Column(db.String(32))
    # 密码
    passwd = db.Column(db.String(32))
    # 员工信息外键
    infoID = db.Column(db.Integer, db.ForeignKey('personinfo.id'))
    person_personinfo = db.relationship(
        'PersonInfo',
        uselist=False,
        backref='personinfo_person'
    )


# 员工信息
class PersonInfo(BaseModel):
    __tablename__ = 'personinfo'
    # 昵称
    nickname = db.Column(db.String(32))
    # 性别
    gender = db.Column(db.Boolean)
    # 年龄
    age = db.Column(db.Integer)
    # 工号
    jobNum = db.Column(db.String(32))
    # 电话
    phone = db.Column(db.String(32))
    # 邮箱
    email = db.Column(db.String(128))
    # 照片
    photo = db.Column(db.String(128))
    # 地址
    address = db.Column(db.String(64))

    # 绩效外键 对应 Kpi
    kpi = db.Column(db.Integer, db.ForeignKey("kpi.id"))
    personinfo_kpi = db.relationship(
        'Kpi',
        backref='kpi_personinfo'
    )

    # 职位外键 对应 Position
    position = db.Column(db.Integer, db.ForeignKey("position.id"))
    personinfo_pos = db.relationship(
        'Position',
        backref='pos_personinfo'
    )

    # 部门外键 对应 Department
    department = db.Column(db.Integer, db.ForeignKey("department.id"))
    personinfo_dep = db.relationship(
        'Department',
        backref='dep_personinfo'
    )


# 新闻
class News(BaseModel):
    # 标题
    title = db.Column(db.String(128))
    # 时间
    releaseTime = db.Column(db.DateTime)
    # 作者
    author = db.Column(db.String(32))
    # 内容
    content = db.Column(db.Text)
    # 图片
    newsImg = db.Column(db.String(256))


# 权限
class Competence(BaseModel):
    # 名称
    name = db.Column(db.String(32))
    # 描述
    desc = db.Column(db.String(128))

    # 职员关系
    competence_personinfo = db.relationship(
        'PersonInfo',
        secondary='com_personinfo',
        backref='personinfo_competence'
    )

    # 职位关系
    competence_position = db.relationship(
        'Position',
        secondary='com_position',
        backref='position_competence'
    )


# 权限 员工 关系表
com_personinfo = db.Table(
    'com_personinfo',
    db.Column('id', db.Integer, primary_key=True, autoincrement=True),
    # 权限 id
    db.Column('com_id', db.Integer, db.ForeignKey('competence.id')),
    # 员工 id
    db.Column('personinfo_id', db.Integer, db.ForeignKey('personinfo.id'))
)

# 权限 职位 关系表
com_position = db.Table(
    'com_position',
    db.Column('id', db.Integer, primary_key=True, autoincrement=True),
    # 权限 id
    db.Column('com_id', db.Integer, db.ForeignKey('competence.id')),
    # 职位 id
    db.Column('position_id', db.Integer, db.ForeignKey('position.id'))
)


# 考勤
class Attendance(BaseModel):
    # 请假人
    people = db.Column(db.String(32))
    # 原因
    reason = db.Column(db.String(128))
    # 类型
    type = db.Column(db.String(32))
    # 假期长度
    length = db.Column(db.Float)
    # 起始时间
    startTime = db.Column(db.DateTime)
    # 结束时间
    stopTime = db.Column(db.DateTime)
    # 考勤状态
    status = db.Column(db.Integer)
    # 审核人
    reviewer = db.Column(db.String(32))

    # 职员 外键 对应 Person_info 表 1:n
    personinfoID = db.Column(db.Integer, db.ForeignKey('personinfo.id'))

    # 对应 Person_info 表 映射关系
    att_personInfo = db.relationship(
        'PersonInfo',
        backref='perinfo_att'
    )


# 部门数据库
class Department(BaseModel):
    # 部门名
    name = db.Column(db.String(32))
    # 描述
    desc = db.Column(db.String(256))


# 职位数据库
class Position(BaseModel):
    # 职位名
    name = db.Column(db.String(32))
    # 描述
    desc = db.Column(db.String(256))


# 绩效
class Kpi(BaseModel):
    month = db.Column(db.Integer)
    value = db.Column(db.Float)
    k_desc = db.Column(db.Text)
